package com.system.Service.Impl;

import com.common.Utils.JwtUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.system.Entity.Announcement;
import com.system.Mapper.AnnouncementMapper;
import com.system.Service.AnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

@Service
public class AnnouncementServiceImpl implements AnnouncementService {
    @Autowired
    private AnnouncementMapper announcementMapper;

    @Override
    public PageInfo<Announcement> getAnnouncementList(Integer pageNum, Integer pageSize, String title, String status) {
        PageHelper.startPage(pageNum,pageSize);
        List<Announcement> announcementList = announcementMapper.getAnnouncementList(title,status);
        return new PageInfo<>(announcementList);
    }

    @Override
    public void addAnnouncement(Announcement announcement) {
        String token=announcement.getToken();
        String username= JwtUtil.extractUsername(token);
        announcement.setCreateUser(username);
        announcement.setCreateTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
        announcement.setUpdateUser(username);
        announcement.setUpdateTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
        announcement.setAnnouncementId(MakeId());
        announcementMapper.addAnnouncement(announcement);

    }



    @Override
    public void updateAnnouncement(Announcement announcement) {
        String token=announcement.getToken();
        String username= JwtUtil.extractUsername(token);
        announcement.setUpdateUser(username);
        announcement.setUpdateTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
        announcementMapper.updateAnnouncement(announcement);
    }

    @Override
    public void deleteAnnouncement(String announcementId) {

        announcementMapper.deleteAnnouncement(announcementId);
    }

    @Override
    public void updateStatus(Announcement announcement) {
        String token=announcement.getToken();
        String username= JwtUtil.extractUsername(token);
        announcement.setUpdateUser(username);
        announcement.setUpdateTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
        announcementMapper.updateStatus(announcement);
    }

    @Override
    public List<Announcement> getAnnouncements() {
        return  announcementMapper.getAnnouncements();
    }

    private String MakeId() {
        return "Am" + System.currentTimeMillis();
    }
}
